package com.source.utils;

import org.springframework.stereotype.Component;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.Calendar;
import java.util.Date;

@Component
public class DateUtils {

    public String getYear() {
        Date date = new Date();
        SimpleDateFormat yyyy = new SimpleDateFormat("yyyy");
        String year = yyyy.format(date);
        return year;
    }

    public String getDate() {
        Date date = new Date();
        SimpleDateFormat formatDate = new SimpleDateFormat("yyyy-MM-dd");
        String day = formatDate.format(date);
        return day;
    }

    public String getMiniute() {
        Date date = new Date();
        SimpleDateFormat formatDate = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        String minute = formatDate.format(date);
        return minute;
    }

    public int getHm() {
        Date date = new Date();
        SimpleDateFormat formatDate = new SimpleDateFormat("HHmm");
        String hm = formatDate.format(date);
        Integer integer = Integer.valueOf(hm);
        return integer;
    }

    public String getBeforeDaysDate(Integer days) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        calendar.add(Calendar.DATE, -days);
        Date date = calendar.getTime();
        SimpleDateFormat formatDate = new SimpleDateFormat("yyyy-MM-dd");
        return formatDate.format(date);
    }

    public Date getDateBeforeDaysDate(Integer days, Date sourceDate) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(sourceDate);
        calendar.add(Calendar.DATE, -days);
        Date date = calendar.getTime();
        SimpleDateFormat formatDate = new SimpleDateFormat("yyyy-MM-dd");
        return stringToDate(formatDate.format(date));
    }

    public Date getDateAfterDaysDate(Integer days, Date sourceDate) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(sourceDate);
        calendar.add(Calendar.DATE, days);
        Date date = calendar.getTime();
        SimpleDateFormat formatDate = new SimpleDateFormat("yyyy-MM-dd");
        return stringToDate(formatDate.format(date));
    }

    public Date getToday() {
        // 和直接new Date() 比就是时分秒都变0
        return stringToDate(new SimpleDateFormat("yyyy-MM-dd").format(new Date()));
    }

    public Date stringToDate(String str) {
        SimpleDateFormat formatDate = new SimpleDateFormat("yyyy-MM-dd");
        Date parse = null;
        try {
            parse = formatDate.parse(str);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return parse;
    }

    public String stringToYMD(Date date) {
        SimpleDateFormat formatDate = new SimpleDateFormat("yyyy-MM-dd");
        String str ="";
        str = formatDate.format(date);
        System.out.println(str);
        return str;
    }

    /**
     * 将当前的日期转成16进制字符串
     *
     * @param date 时间
     * @return String
     */
    public static String datetoHex(LocalDateTime date) {
        StringBuffer sb = new StringBuffer();

        // 年月日 时分秒
        sb.append(Integer.toHexString(date.getYear() % 1000));

        String monthHex = Integer.toHexString(date.getMonthValue());
        sb.append(monthHex.length() == 1 ? 0 + monthHex : monthHex);

        String dayHex = Integer.toHexString(date.getDayOfMonth());
        sb.append(dayHex.length() == 1 ? 0 + dayHex : dayHex);

        String hourHex = Integer.toHexString(date.getHour());
        sb.append(hourHex.length() == 1 ? 0 + hourHex : hourHex);

//        String minuteHex = Integer.toHexString(date.getMinute());
//        sb.append(minuteHex.length() == 1 ? 0 + minuteHex : minuteHex);
//
//        String secondHex = Integer.toHexString(date.getSecond());
//        sb.append(secondHex.length() == 1 ? 0 + secondHex : secondHex);

        return sb.toString();
    }

    public static void main(String[] args) {
        System.out.println(datetoHex(LocalDateTime.of(
                LocalDate.of(2021, 1, 28),
                LocalTime.of(22, 43, 0)
        )));
    }
}
